# -*- coding: utf-8 -*-

"""
Module implementing MainWindow.
"""

import os, sys
#from PySide2.QtCore import Slot as pyqtSlot
from PyQt5.QtCore import pyqtSlot
#from PySide2.QtWidgets import QMainWindow, QMessageBox
from PyQt5.QtWidgets import QMainWindow, QMessageBox
#from PySide2 import QtPrintSupport, QtWidgets
from PyQt5 import QtPrintSupport, QtWidgets
from Ui_main import Ui_MainWindow

import win32print
import win32com.client

class MainWindow(QMainWindow, Ui_MainWindow):
    """
    Class documentation goes here.
    """
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget
        @type QWidget
        """
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
    
    @pyqtSlot(bool)
    def on_radioButton_clicked(self, checked):
        self.selectLineEdit = self.lineEdit
        self.queryField = 'Jobno'
        if self.label_4.text() == "请输入转单号":
            self.label_4.setText("请输入原单号")
        self.lineEdit.setEnabled(True)        
        self.lineEdit_3.setDisabled(True)
        if self.isCheckNumBind == False:
            self.lineEdit_3.clear()
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(bool)
    def on_radioButton_2_clicked(self, checked):
        self.selectLineEdit = self.lineEdit_3
        self.queryField = 'Refno'
        if self.label_4.text() == "请输入原单号":
            self.label_4.setText("请输入转单号")
        if self.isCheckNumBind == False:
            self.lineEdit.clear()
        self.lineEdit.setDisabled(True)
        self.lineEdit_3.setEnabled(True)
        self.selectLineEdit.setFocus()
    
    @pyqtSlot()
    def on_lineEdit_returnPressed(self):
        self.lineEditInputProcess('originalSN')
    
    @pyqtSlot()
    def on_pushButton_2_clicked(self):
        self.writeToClipboard(self.existedShipmentAll, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
    
    @pyqtSlot()
    def on_pushButton_3_clicked(self):
        self.saveAsExcel(self.existedShipmentAll, [['','总清单']])
    
    @pyqtSlot(bool)
    def on_radioButton_5_clicked(self, checked):
        if self.selectAction != 'rb5':
            self.selectAction = 'rb5'
            self.selectRadioButtonAlias = self.radioButton_5.text()
            self.customStringReplaceList = []
            self.readStringReplaceSetting(self.selectAction, self.customStringReplaceList)
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(bool)
    def on_radioButton_6_clicked(self, checked):
        if self.selectAction != 'rb6':
            self.selectAction = 'rb6'
            self.selectRadioButtonAlias = self.radioButton_6.text()
            self.customStringReplaceList = []
            self.readStringReplaceSetting(self.selectAction, self.customStringReplaceList)
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(bool)
    def on_radioButton_7_clicked(self, checked):
        if self.selectAction != 'rb7':
            self.selectAction = 'rb7'
            self.selectRadioButtonAlias = self.radioButton_7.text()
            self.customStringReplaceList = []
            self.readStringReplaceSetting(self.selectAction, self.customStringReplaceList)
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(bool)
    def on_radioButton_11_clicked(self, checked):
        if self.selectAction != 'rb11':
            self.selectAction = 'rb11'
            self.selectRadioButtonAlias = self.radioButton_11.text()
            self.customStringReplaceList = []
            self.readStringReplaceSetting(self.selectAction, self.customStringReplaceList)
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(bool)
    def on_radioButton_8_clicked(self, checked):
        if self.selectAction != 'rb8':
            self.selectAction = 'rb8'
            self.selectRadioButtonAlias = self.radioButton_8.text()
            self.customStringReplaceList = []
            self.readStringReplaceSetting(self.selectAction, self.customStringReplaceList)
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(bool)
    def on_radioButton_9_clicked(self, checked):
        if self.selectAction != 'rb9':
            self.selectAction = 'rb9'
            self.selectRadioButtonAlias = self.radioButton_9.text()
            self.customStringReplaceList = []
            self.readStringReplaceSetting(self.selectAction, self.customStringReplaceList)
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(bool)
    def on_radioButton_13_clicked(self, checked):
        if self.selectAction != 'rb13':
            self.selectAction = 'rb13'
            self.selectRadioButtonAlias = self.radioButton_13.text()
            self.customStringReplaceList = []
            self.readStringReplaceSetting(self.selectAction, self.customStringReplaceList)
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(bool)
    def on_radioButton_10_clicked(self, checked):
        if self.selectAction != 'rb10':
            self.selectAction = 'rb10'
            self.selectRadioButtonAlias = self.radioButton_10.text()
            self.customStringReplaceList = []
            self.readStringReplaceSetting(self.selectAction, self.customStringReplaceList)
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(bool)
    def on_radioButton_3_clicked(self, checked):
        if self.selectAction != 'rb3':
            self.selectAction = 'rb3'
            self.selectRadioButtonAlias = self.radioButton_3.text()
            self.customStringReplaceList = []
            self.readStringReplaceSetting(self.selectAction, self.customStringReplaceList)
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(bool)
    def on_radioButton_12_clicked(self, checked):
        if self.selectAction != 'rb12':
            self.selectAction = 'rb12'
            self.selectRadioButtonAlias = self.radioButton_12.text()
            self.customStringReplaceList = []
            self.readStringReplaceSetting(self.selectAction, self.customStringReplaceList)
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(bool)
    def on_radioButton_4_clicked(self, checked):
        if self.selectAction != 'rb4':
            self.selectAction = 'rb4'
            self.selectRadioButtonAlias = self.radioButton_4.text()
            self.customStringReplaceList = []
            self.readStringReplaceSetting(self.selectAction, self.customStringReplaceList)
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(bool)
    def on_radioButton_14_clicked(self, checked):
        if self.selectAction != 'rb14':
            self.selectAction = 'rb14'
            self.selectRadioButtonAlias = self.radioButton_14.text()
            self.customStringReplaceList = []
            self.readStringReplaceSetting(self.selectAction, self.customStringReplaceList)
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(bool)
    def on_radioButton_15_clicked(self, checked):
        if self.selectAction != 'rb15':
            self.selectAction = 'rb15'
            self.selectRadioButtonAlias = self.radioButton_15.text()
            self.customStringReplaceList = []
            self.readStringReplaceSetting(self.selectAction, self.customStringReplaceList)
        self.selectLineEdit.setFocus()
    
    @pyqtSlot()
    def on_pushButton_4_clicked(self):
        self.maniClassPrint(self.existedShipmentAll)
    
    @pyqtSlot()
    def on_pushButton_clicked(self):
        self.writeToClipboard(self.existedShipmentAll, [self.tableFieldDict['Jobno'][0]])
    
    @pyqtSlot()
    def on_pushButton_5_clicked(self):
        for o1a in self.groupBox_2.findChildren(QtWidgets.QGroupBox):
            self.delClasslManifest(o1a.findChild(QtWidgets.QTableWidget))
        for n1a in range(self.tableWidget.rowCount(), 0, -1):
            self.tableWidget.removeRow(n1a - 1)
        self.existedShipmentAll.clear()
        self.label_12.setText('0')
    
    @pyqtSlot(int)
    def on_checkBox_stateChanged(self, p0):
        if p0 == 0:
            self.filterHubin = 0
        elif p0 == 2:
            self.filterHubin = 1
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(int)
    def on_checkBox_2_stateChanged(self, p0):
        if p0 == 0:
            self.filterHubout = 0
        elif p0 == 2:
            self.filterHubout = 1
        self.selectLineEdit.setFocus()
    
    @pyqtSlot()
    def on_action_triggered(self):
        self.openSettingWindow('checkList','limitHubIn', '设置限定指定路线')
    
    @pyqtSlot()
    def on_action_2_triggered(self):
        self.openSettingWindow('checkList','limitHubOut', '设置限定走货路线')
    
    @pyqtSlot()
    def on_action_3_triggered(self):
        self.openSettingWindow('stringReplace','generalStringReplace', '通用播报字句替换')
    
    @pyqtSlot()
    def on_action_4_triggered(self):
        self.openSettingWindow('stringReplace', self.selectAction, self.selectRadioButtonAlias)
    
    @pyqtSlot(int)
    def on_checkBox_3_stateChanged(self, p0):
        if p0 == 0:
            self.isDestNameNotify = 0
        elif p0 == 2:
            self.isDestNameNotify = 1
        self.selectLineEdit.setFocus()
    
    @pyqtSlot()
    def on_radioButton_16_clicked(self):
        self.isAddInManifest = True
        self.selectLineEdit.setFocus()
    
    @pyqtSlot()
    def on_radioButton_17_clicked(self):
        self.isAddInManifest = False
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(int)
    def on_checkBox_4_stateChanged(self, p0):
        if p0 == 0:
            if ['Hubin', '指定路线'] in self.maniClassCondi:
                self.maniClassCondi.remove(['Hubin', '指定路线'])
        elif p0 == 2:
            if ['Hubin', '指定路线'] not in self.maniClassCondi:
                self.maniClassCondi.append(['Hubin', '指定路线'])
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(int)
    def on_checkBox_5_stateChanged(self, p0):
        if p0 == 0:
            if ['Hubout', '走货路线'] in self.maniClassCondi:
                self.maniClassCondi.remove(['Hubout', '走货路线'])
        elif p0 == 2:
            if ['Hubout', '走货路线'] not in self.maniClassCondi:
                self.maniClassCondi.append(['Hubout', '走货路线'])
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(int)
    def on_checkBox_6_stateChanged(self, p0):
        if p0 == 0:
            if ['delistation', '下家代码'] in self.maniClassCondi:
                self.maniClassCondi.remove(['delistation', '下家代码'])
        elif p0 == 2:
            if ['delistation', '下家代码'] not in self.maniClassCondi:
                self.maniClassCondi.append(['delistation', '下家代码'])
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(int)
    def on_checkBox_7_stateChanged(self, p0):
        if p0 == 0:
            if ['IsAuditingYF', '是否出货'] in self.maniClassCondi:
                self.maniClassCondi.remove(['IsAuditingYF', '是否出货'])
            if ['IsAuditingJobno', '是否审单'] in self.maniClassCondi:
                self.maniClassCondi.remove(['IsAuditingJobno', '是否审单'])
        elif p0 == 2:
            if ['IsAuditingYF', '是否出货'] not in self.maniClassCondi:
                self.maniClassCondi.append(['IsAuditingYF', '是否出货'])
            if ['IsAuditingJobno', '是否审单'] not in self.maniClassCondi:
                self.maniClassCondi.append(['IsAuditingJobno', '是否审单'])
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(int)
    def on_checkBox_8_stateChanged(self, p0):
        if p0 == 0:
            if ['selectedHubin', '是否限定指定路线'] in self.maniClassCondi:
                self.maniClassCondi.remove(['selectedHubin', '是否限定指定路线'])
        elif p0 == 2:
            if ['selectedHubin', '是否限定指定路线'] not in self.maniClassCondi:
                self.maniClassCondi.append(['selectedHubin', '是否限定指定路线'])
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(int)
    def on_checkBox_9_stateChanged(self, p0):
        if p0 == 0:
            if ['selectedHubout', '是否限定走货路线'] in self.maniClassCondi:
                self.maniClassCondi.remove(['selectedHubout', '是否限定走货路线'])
        elif p0 == 2:
            if ['selectedHubout', '是否限定走货路线'] not in self.maniClassCondi:
                self.maniClassCondi.append(['selectedHubout', '是否限定走货路线'])
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(int)
    def on_checkBox_10_stateChanged(self, p0):
        if p0 == 0:
            if ['destNameNotify', '是否提醒目的地'] in self.maniClassCondi:
                self.maniClassCondi.remove(['destNameNotify', '是否提醒目的地'])
        elif p0 == 2:
            if ['destNameNotify', '是否提醒目的地'] not in self.maniClassCondi:
                self.maniClassCondi.append(['destNameNotify', '是否提醒目的地'])
        self.selectLineEdit.setFocus()
    
    @pyqtSlot(int)
    def on_checkBox_11_stateChanged(self, p0):
        if p0 == 0:
            if ['SDStation', '站点'] in self.maniClassCondi:
                self.maniClassCondi.remove(['SDStation', '站点'])
        elif p0 == 2:
            if ['SDStation', '站点'] not in self.maniClassCondi:
                self.maniClassCondi.append(['SDStation', '站点'])
        self.selectLineEdit.setFocus()
    
    @pyqtSlot()
    def on_pushButton_6_clicked(self):
        if self.pushButton_6.text() == "隐藏设置选项":
            self.groupBox_3.hide()
            self.pushButton_6.setText("显示设置选项")
        elif self.pushButton_6.text() == "显示设置选项":
            self.scrollArea.setMinimumHeight(0)
            self.groupBox_3.show()
            self.pushButton_6.setText("隐藏设置选项")
        self.selectLineEdit.setFocus()
    
    @pyqtSlot()
    def on_pushButton_7_clicked(self):        
        win32print.SetDefaultPrinter(self.comboBox_5.currentText())
        for l1a in self.classManiDataList:
            u2a = self.exportToExcel(l1a, self.printTemplate)
            if u2a != False:                
                try:
                    xlApp = win32com.client.Dispatch('Excel.Application')
                    workbook = xlApp.Workbooks.open(os.path.split(os.path.abspath(sys.argv[0]))[0] + "\printtemplate\\temp\\" + u2a)                
                    workbook.PrintOut()
                    workbook.Close(SaveChanges=0)
                    del xlApp
                except:
                    QMessageBox.warning(self.centralWidget,'未知错误','打印失败!', QMessageBox.Ok)
        win32print.SetDefaultPrinter(self.sysDefaultPrinter)
    
    @pyqtSlot()
    def on_pushButton_8_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        raise NotImplementedError
    
    @pyqtSlot(int)
    def on_comboBox_4_activated(self, index):
        l1a = []
        for n1a in range(self.comboBox_4.count()):
            l1a.append([self.comboBox_4.itemText(n1a), False])
        for s1a in os.listdir(os.path.split(os.path.abspath(sys.argv[0]))[0] + "\printtemplate"):
            if not os.path.isdir(s1a):
                if os.path.splitext(s1a)[1] == '.xls' or os.path.splitext(s1a)[1] == '.xlsx':
                    c4a = False
                    for i4a, l4a in enumerate(l1a):
                        if l4a[0] == s1a:
                            l1a[i4a][1] = True
                            c4a = True
                    if c4a == False:
                        self.comboBox_4.addItem(s1a)
        n1a = 0
        c2a = False
        for i1a in range(len(l1a)):            
            if l1a[i1a][1] == False:
                self.comboBox_4.removeItem(i1a - n1a)
                n1a += 1
                c2a == True
        self.printTemplate = self.comboBox_4.currentText()
    
    @pyqtSlot(int)
    def on_comboBox_5_activated(self, index):
        #如有新发现打印机则加入combobox
        l1a = []
        for n1a in range(self.comboBox_5.count()):
            #每个列表前一个为名称字符串，后一个为状态
            l1a.append([self.comboBox_5.itemText(n1a), False])
        for s1a in QtPrintSupport.QPrinterInfo().availablePrinterNames():
            #如果匹配到原combobox内容，则combobox列表相应项状态刷新为True
            c2a = False
            for i2a, l2a in enumerate(l1a):
                if l2a[0] == s1a:
                    l1a[i2a][1] = True
                    c2a = True
            #未匹配到则添加
            if c2a == False:
                self.comboBox_4.addItem(s1a)
                self.printersList.append(s1a)
        #如combobox列表中有状态依然为False的打印机，则删除
        n1a = 0
        c1a = False
        for i1a in range(len(l1a)):            
            if l1a[i1a][1] == False:
                self.comboBox_5.removeItem(i1a - n1a)
                del self.printersList[i1a - n1a]
                n1a += 1
                c1a == True
        self.sysDefaultPrinter = QtPrintSupport.QPrinterInfo().defaultPrinterName()
    
    @pyqtSlot()
    def on_action_5_triggered(self):
        self.openSettingWindow('checkList','notifyDest', '设置目的地提醒')
    
    @pyqtSlot()
    def on_lineEdit_2_returnPressed(self):
        self.findAndSelectTableWidgetRow(self.tableWidget, self.existedShipmentAll, self.tableFieldDict['Jobno'][0])
    
    @pyqtSlot(int)
    def on_checkBox_12_stateChanged(self, p0):
        if p0 == 2:
            self.isCheckNumBind = True
        elif p0 == 0:
            self.isCheckNumBind = False
        self.selectLineEdit.setFocus()
    
    @pyqtSlot()
    def on_lineEdit_3_returnPressed(self):
        self.lineEditInputProcess('forwardSN')
